package mommy;

public class Mommy {

    public String mommyfy(String input) {
        if (input.length() > 0 && percentVowelsIsThirty(input)) {
            return addLetterOrMommyToMommifyWord(input);
        }
        return input;
    }

    private String addLetterOrMommyToMommifyWord(String input) {
        String mommy = "mommy";
        String result = "";

        for(char letter : input.toCharArray()) {
            if (!isVowel(letter)) {
                result += letter;
            } else {
                if (!result.endsWith(mommy))
                    result += mommy;
            }
        }
        return result;
    }

    private boolean percentVowelsIsThirty(String input) {
        int percentageMajority = 30;

        if (countHowManyPercentVowelsInAnWord(input) > percentageMajority)
            return true;

        return false;
    }

    private double countHowManyPercentVowelsInAnWord(String input) {
        double amountVowels = 0;

        for(char letter : input.toCharArray()) {
            if(isVowel(letter))
                amountVowels++;
        }
        return (amountVowels / input.length()) * 100;
    }

    private boolean isVowel(char letter) {
        String regex = ".*[AaEeIiOoUu].*";
        return String.valueOf(letter).matches(regex);
    }
}
